package ahxu;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import ahxu.crawler.CrawlServiceProvider;
import ahxu.search.db.SearchServiceProvider;

public class AhxuServiceProvider {
private static AhxuServiceProvider serviceProvider = null;
	
	private static ClassPathXmlApplicationContext classPathXmlApplicationContext = null;
	private static CrawlServiceProvider crawlServiceProvider;
	private static SearchServiceProvider searchServiceProvider;
	
	private static String[] contextXml = new String[] {
		"classpath*:resources/*-spring-core.xml",
		"classpath*:resources/crawl/*-spring-core.xml",
		"classpath*:resources/search/*-spring-core.xml"
	};

	private AhxuServiceProvider() {
		super();
		initContext();
	}

	private void initContext() {
		classPathXmlApplicationContext = 
			new ClassPathXmlApplicationContext(contextXml);
		crawlServiceProvider=CrawlServiceProvider.getInstance(classPathXmlApplicationContext);
		searchServiceProvider=SearchServiceProvider.getInstance(classPathXmlApplicationContext);
	}
	
	public static AhxuServiceProvider getInstance() {
		if(serviceProvider==null) {
			serviceProvider = new AhxuServiceProvider();
		}
		return serviceProvider;
	}
	
	public CrawlServiceProvider getCrawlServiceProvider(){
		return this.crawlServiceProvider;
	}
	public SearchServiceProvider getSearchServiceProvider(){
		return this.searchServiceProvider;
	}

	
	
	
	 @SuppressWarnings("unchecked")
	private <T> T getCommonBean(String name,Class<T> type){
		return (T)classPathXmlApplicationContext.getBean(name);
	}
}
